<?php
namespace app\facility\controller;

use think\Cache;
use think\Controller;
use think\Log;
use think\Request;
use think\Db;
use app\index\model\CollectMobileInfo;

class MacMessageConsume extends Controller
{
    public function process_message($message)
    {
        try {
            // $obj = json_decode($message);
            $temp_array = explode("&", $message);
            $len = sizeof($temp_array);
            if ($len == 4 || $len == 5) {
                //write_log("mac data151:" . $message, 1);
                $device_id = $temp_array[0];
                $temp = $temp_array[2];
                $now_time_num = $temp_array[3];
                $mac_list = $temp_array[1];
                $mobileData = explode(";", $mac_list);
                $curtime = time();
                $mac_info = "";
                if ($len == 5) {
                    $sql = "insert into gprs_device_operation_record(device_id,send_num,add_time) values('" . $device_id
                        . "','" . $temp_array[4] . "','" . date('Y-m-d H:i:s') . "');";
                    Db::execute($sql);
                    echo "work num：" . $temp_array[4] . "\n";
                }
                if (sizeof($mobileData) > 1) {
                    $preg = "/^([0-9a-fA-F]{2}:)(([0-9a-fA-F]{2}:){4})([0-9a-fA-F]{2})$/";
                    foreach ($mobileData as $item) {
                        if ($item != "") {
                            $temp_array2 = explode("|", $item);
                            if (sizeof($temp_array2) == 4 && preg_match($preg, $temp_array2[0])) {
                                $t1 = $now_time_num - $temp_array2[3];
                                $t2 = $curtime - $t1;
                                $mac_info .= $temp_array2[0] . "|" . $temp_array2[1] . "|" . $temp_array2[2] . "|" . date("Y-m-d H:i:s", $t2) . ";";
                            }
                            if (sizeof($temp_array2) == 2 && preg_match($preg, $temp_array2[0])) {
                                $t1 = $now_time_num - $temp_array2[1];
                                $t2 = $curtime - $t1;
                                $mac_info .= "0|0|" . $temp_array2[0] . "|" . date("Y-m-d H:i:s", $t2) . ";";
                            }
                        }
                    }
                }
                $data = "{\"id\":\"" . $device_id . "\",\"data\":\"" . $mac_info . "\",\"temp\":\"" . $temp . "\"}";
                $data = json_decode($data);
                $this->SaveWifiData($data);
                
            } else {
                echo 'error data\n';
                write_log("error data:" . $message, 2);
            }

        } catch (\Think\Exception  $outExcep) {
            write_log("处理数据出异常了：" . $outExcep->getMessage(), 2);
        }
    }

    private function SaveWifiData($data)
    {
        if (!isset($data->id)) {
            write_log("错误的mac信息" . json_encode($data), 2);
            return;
        }
        $deviceInfo = $this->get_device_info($data->id);
        if (!$deviceInfo) {
            return "no";
        }
        $billboard_id = $deviceInfo[0];

        if ($data->temp && $data->temp != "0" && $data->temp != "-127.00" && $data->temp != "85") {
            $temperature = Cache::get("billboard_temp_" . $billboard_id);
            if (!$temperature) {
                //&& $data->temp != "85.00" && $data->temp != "0" && $data->temp != "0.00"
                $sql = "insert into billboard_temp_" . $billboard_id
                    . "(temp,add_time) values('" . $data->temp . "','" .
                    date('Y-m-d H:i:s') . "')";
                Db::execute($sql);
                Cache::set("billboard_temp_" . $billboard_id, $data->temp, 60);
            }
        }
        if ($data->data == "") {
            return "no";
        }
        $mobileData = explode(";", $data->data);
        if (sizeof($mobileData) > 1) {
            foreach ($mobileData as $item) {
                if ($item != "") {
                    $this->addSubData($billboard_id, $item);
                }
            }
        }
        return "ok";
    }

    private function addSubData($pid, $data)
    {
        $arraylist = explode("|", $data);
        if (sizeof($arraylist) == 4) {
            $rssi = $arraylist[0];
            $ch = $arraylist[1];
            $mac = $arraylist[2];
            $collect_time = $arraylist[3];
            $arrMac = explode(":", $mac);
            if (sizeof($arrMac) != 6) {
                write_log($mac . " " . date("Y-m-d H:i:s"), 4);
                return;
            }
            $mac_id = Cache::get("mac_" . $mac);
            if (!$mac_id) {
                $vendor_id = Cache::get("vendor_mac_" . substr($mac, 0, 8));
                if (!$vendor_id) {
                    $vendor_id = -1;
                }
                $m = new CollectMobileInfo;
                $m->vendor_id = $vendor_id;
                $m->mac = $mac;
                $m->add_time = $collect_time;
                $m->save();
                $mac_id = $m->id;
                //write_log($data."<".$arraylist[2].">"."      "$m->getLastSql().,4);
                Cache::set("mac_" . $mac, $mac_id);
            }
            $sql = "insert into billboard_mac_" . $pid
                . "(mac_id,add_time,channel,rssi,collect_time) values('" . $mac_id . "','" .
                date('Y-m-d H:i:s') . "','" . $ch . "','" . $rssi . "','" . $collect_time . "')";
            Db::execute($sql);

            // 保存缓存查询设备连接情况
            $update_bil = Cache::get("update_billboard_" . $pid);
            $img_bil = Cache::get("img_billboard_" . $pid);
            $type = 1;
            $device_type = 1;
            if ($update_bil) {
                $type = $update_bil['type'] == 2 ? 3 : ($update_bil['type'] == 3 ? 3 : 1);
            }
            if ($img_bil) {
                $device_type = ($img_bil['type'] == 2) ? 2 : 1;
            }
            Cache::set("update_billboard_" . $pid, array('time' => time() + 60 * 5, 'type' => $type, 'device_type' => $device_type));
            // 保存mac轨迹重复率
            $mac_track = Cache::get("billboard_mac_track_" . $pid . "_" . $mac);
            if (!$mac_track) {
                $trackSql = "insert into billboard_mac_track_" . $pid . "(mac_id,quantity,add_time,collect_time) values('" . $mac_id . "','1','" . date('Y-m-d H:i:s') . "','" . $collect_time . "')";
                Cache::set("billboard_mac_track_" . $pid . "_" . $mac, $mac_id);
            } else {
                $trackSql = "update billboard_mac_track_" . $pid . " set quantity=quantity+1, collect_time='" . date('Y-m-d H:i:s') . "' where mac_id=" . $mac_id;
            }
            Db::execute($trackSql);

            $outline_mac = Cache::get("billboard_mac_" . $mac);
            if (!$outline_mac) {
                $sql = "insert into billboard_mac_outline_" . $pid
                    . "(mac_id,add_time,collect_time) values('" . $mac_id . "','" .
                    date('Y-m-d H:i:s') . "','" . $collect_time . "')";
                Db::execute($sql);
                Cache::set("billboard_mac_" . $mac, "o", 60 * 30);
            }
        }
    }

    public function get_device_info($device_id)
    {
        $sql = "SELECT id,device_id,firmware_version FROM  billboard where device_id='" . $device_id . "'";
        $data = Db::query($sql);
        if (sizeof($data) > 0) {
            $deviceInfo = array($data[0]["id"], $data[0]["firmware_version"]);
        }
        return $deviceInfo;
    }

 



}
